<?php

namespace com;

/**
 * 雅黑探针类文件
 */

class YHprobe
{
    //linux系统探测
    public function sys_linux()
    {
        // CPU
        if (false === ($str = @file("/proc/cpuinfo"))) return false;
        $str = implode("", $str);
        @preg_match_all("/model\s+name\s{0,}\:+\s{0,}([\w\s\)\(\@.-]+)([\r\n]+)/s", $str, $model);
        @preg_match_all("/cpu\s+MHz\s{0,}\:+\s{0,}([\d\.]+)[\r\n]+/", $str, $mhz);
        @preg_match_all("/cache\s+size\s{0,}\:+\s{0,}([\d\.]+\s{0,}[A-Z]+[\r\n]+)/", $str, $cache);
        @preg_match_all("/bogomips\s{0,}\:+\s{0,}([\d\.]+)[\r\n]+/", $str, $bogomips);
        if (false !== is_array($model[1])) {
                $res['cpu']['num'] = sizeof($model[1]);
                /*
        for($i = 0; $i < $res['cpu']['num']; $i++)
        {
            $res['cpu']['model'][] = $model[1][$i].'&nbsp;('.$mhz[1][$i].')';
            $res['cpu']['mhz'][] = $mhz[1][$i];
            $res['cpu']['cache'][] = $cache[1][$i];
            $res['cpu']['bogomips'][] = $bogomips[1][$i];
        }*/
                if ($res['cpu']['num'] == 1)
                    $x1 = '';
                else
                    $x1 = ' ×' . $res['cpu']['num'];
                $mhz[1][0] = ' | 频率:' . $mhz[1][0];
                $cache[1][0] = ' | 二级缓存:' . $cache[1][0];
                $bogomips[1][0] = ' | Bogomips:' . $bogomips[1][0];
                $res['cpu']['model'][] = $model[1][0] . $mhz[1][0] . $cache[1][0] . $bogomips[1][0] . $x1;
                if (false !== is_array($res['cpu']['model'])) $res['cpu']['model'] = implode("<br />", $res['cpu']['model']);
                if (false !== is_array($res['cpu']['mhz'])) $res['cpu']['mhz'] = implode("<br />", $res['cpu']['mhz']);
                if (false !== is_array($res['cpu']['cache'])) $res['cpu']['cache'] = implode("<br />", $res['cpu']['cache']);
                if (false !== is_array($res['cpu']['bogomips'])) $res['cpu']['bogomips'] = implode("<br />", $res['cpu']['bogomips']);
            }

        // NETWORK

        // UPTIME
        if (false === ($str = @file("/proc/uptime"))) return false;
        $str = explode(" ", implode("", $str));
        $str = trim($str[0]);
        $min = $str / 60;
        $hours = $min / 60;
        $days = floor($hours / 24);
        $hours = floor($hours - ($days * 24));
        $min = floor($min - ($days * 60 * 24) - ($hours * 60));
        if ($days !== 0) $res['uptime'] = $days . "天";
        if ($hours !== 0) $res['uptime'] .= $hours . "小时";
        $res['uptime'] .= $min . "分钟";

        // MEMORY
        if (false === ($str = @file("/proc/meminfo"))) return false;
        $str = implode("", $str);
        preg_match_all("/MemTotal\s{0,}\:+\s{0,}([\d\.]+).+?MemFree\s{0,}\:+\s{0,}([\d\.]+).+?Cached\s{0,}\:+\s{0,}([\d\.]+).+?SwapTotal\s{0,}\:+\s{0,}([\d\.]+).+?SwapFree\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buf);
        preg_match_all("/Buffers\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buffers);

        $res['memTotal'] = round($buf[1][0] / 1024, 2);
        $res['memFree'] = round($buf[2][0] / 1024, 2);
        $res['memBuffers'] = round($buffers[1][0] / 1024, 2);
        $res['memCached'] = round($buf[3][0] / 1024, 2);
        $res['memUsed'] = $res['memTotal'] - $res['memFree'];
        $res['memPercent'] = (floatval($res['memTotal']) != 0) ? round($res['memUsed'] / $res['memTotal'] * 100, 2) : 0;

        $res['memRealUsed'] = $res['memTotal'] - $res['memFree'] - $res['memCached'] - $res['memBuffers']; //真实内存使用
        $res['memRealFree'] = $res['memTotal'] - $res['memRealUsed']; //真实空闲
        $res['memRealPercent'] = (floatval($res['memTotal']) != 0) ? round($res['memRealUsed'] / $res['memTotal'] * 100, 2) : 0; //真实内存使用率

        $res['memCachedPercent'] = (floatval($res['memCached']) != 0) ? round($res['memCached'] / $res['memTotal'] * 100, 2) : 0; //Cached内存使用率

        $res['swapTotal'] = round($buf[4][0] / 1024, 2);
        $res['swapFree'] = round($buf[5][0] / 1024, 2);
        $res['swapUsed'] = round($res['swapTotal'] - $res['swapFree'], 2);
        $res['swapPercent'] = (floatval($res['swapTotal']) != 0) ? round($res['swapUsed'] / $res['swapTotal'] * 100, 2) : 0;

        // LOAD AVG
        if (false === ($str = @file("/proc/loadavg"))) return false;
        $str = explode(" ", implode("", $str));
        $str = array_chunk($str, 4);
        $res['loadAvg'] = implode(" ", $str[0]);

        return $res;
    }

     //Linux cpu检查
     public function cpu_lix()
     {
         $mode = "/(cpu)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)/";
         $string=shell_exec("more /proc/stat");
         preg_match_all($mode,$string,$arr);
         //print_r($arr);
         $total1=$arr[2][0]+$arr[3][0]+$arr[4][0]+$arr[5][0]+$arr[6][0]+$arr[7][0]+$arr[8][0]+$arr[9][0];
         $time1=$arr[2][0]+$arr[3][0]+$arr[4][0]+$arr[6][0]+$arr[7][0]+$arr[8][0]+$arr[9][0];
         
         sleep(1);
         $string=shell_exec("more /proc/stat");
         preg_match_all($mode,$string,$arr);
         $total2=$arr[2][0]+$arr[3][0]+$arr[4][0]+$arr[5][0]+$arr[6][0]+$arr[7][0]+$arr[8][0]+$arr[9][0];
         $time2=$arr[2][0]+$arr[3][0]+$arr[4][0]+$arr[6][0]+$arr[7][0]+$arr[8][0]+$arr[9][0];
         $time=$time2-$time1;
         $total=$total2-$total1;
         //echo "CPU amount is: ".$num;
         $percent=round($time/$total,3);
         $percent=$percent*100;
         return [
            'percent' => $percent,
            'total' => $total,
            'time' => $time
            ];
     }

     //查看进程状态
     public function getProcessInfo($process_mark){
        $cmd="ps ax |grep $process_mark |grep -v grep|grep -v vi|grep -v dbx   |grep -v tail|grep -v start|grep -v stop |sed -n 1p |awk '{print $1}'";
        $pid= @exec($cmd);
        if(intval($pid)>0){
        //$mem = @exec("ps -o vsz -p $pid |grep -v VSZ");
        //$cpu   = @exec("ps -p $pid  -o pcpu |grep -v CPU | awk '{print $1}'");
        //$cmd=`top -p $pid -n 1 |grep $process_mark 2>&1 > /tmp/1.txt `;
        $top = `top -b -n 1  -p $pid |grep -v grep|grep -v top|grep -v Tasks|grep -v Cpu|grep -v Mem|grep -v Swap|grep -v PID`;
        $rt_str = trim( $top );
        //file_put_contents(_ROOT."t2.txt",$rt_str);
        $rt_str_array = preg_split("/\s+/",$rt_str);
    
        //$cmd="ps  -p $pid -o start  |grep -v grep";
        $cmd="ps  -p $pid -o etime  |grep -v grep";
        $start= exec( $cmd );
        
        return array("pid"=>$rt_str_array[0],
        "etime"=>$start,
        "cmd"=>$rt_str_array[11],
        "cpu"=>$rt_str_array[8],//"mem"=>sprintf("%.02f",$rt_str_array[4]),
        "mem"=>$rt_str_array[4],
        "stat" => $rt_str_array[7],
        );
        }else{
            return false;
        }
    }


}
